package cn.iocoder.yudao.module.reading.controller.app.audio;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.reading.controller.app.audio.vo.AudioPlayReqVO;
import cn.iocoder.yudao.module.reading.controller.app.audio.vo.AudioResourceRespVO;
import cn.iocoder.yudao.module.reading.service.audio.AudioService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 用户 APP - 音频管理
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 音频管理")
@RestController
@RequestMapping("/reading/audio")
@Validated
@Slf4j
public class AudioController {

    @Resource
    private AudioService audioService;

    @GetMapping("/list")
    @Operation(summary = "获取音频资源列表")
    @PreAuthenticated
    public CommonResult<List<AudioResourceRespVO>> getAudioList(
            @Parameter(description = "音频分类", required = true, example = "recent") @RequestParam("category") String category,
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang,
            @Parameter(description = "限制数量", example = "20") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        List<AudioResourceRespVO> result = audioService.getAudioResourcesByCategory(userId, category, lang, limit);
        return success(result);
    }

    @GetMapping("/search")
    @Operation(summary = "搜索音频资源")
    @PreAuthenticated
    public CommonResult<List<AudioResourceRespVO>> searchAudio(
            @Parameter(description = "搜索关键词", required = true, example = "牛津") @RequestParam("keyword") String keyword,
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang) {
        Long userId = getLoginUserId();
        List<AudioResourceRespVO> result = audioService.searchAudioResources(userId, keyword, lang);
        return success(result);
    }

    @GetMapping("/recommended")
    @Operation(summary = "获取推荐音频资源")
    @PreAuthenticated
    public CommonResult<List<AudioResourceRespVO>> getRecommendedAudio(
            @Parameter(description = "语言类型", example = "en") @RequestParam(value = "lang", required = false) String lang,
            @Parameter(description = "限制数量", example = "10") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        List<AudioResourceRespVO> result = audioService.getRecommendedAudioResources(userId, lang, limit);
        return success(result);
    }

    @PostMapping("/play")
    @Operation(summary = "保存音频播放记录")
    @PreAuthenticated
    public CommonResult<Long> savePlayRecord(@Valid @RequestBody AudioPlayReqVO playReqVO) {
        Long userId = getLoginUserId();
        Long recordId = audioService.saveAudioPlayRecord(userId, playReqVO);
        return success(recordId);
    }

    @GetMapping("/recent")
    @Operation(summary = "获取最近播放的音频")
    @PreAuthenticated
    public CommonResult<List<AudioResourceRespVO>> getRecentPlayAudios(
            @Parameter(description = "限制数量", example = "10") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        List<AudioResourceRespVO> result = audioService.getRecentPlayAudios(userId, limit);
        return success(result);
    }

    @DeleteMapping("/clear")
    @Operation(summary = "清空播放记录")
    @PreAuthenticated
    public CommonResult<Boolean> clearPlayRecords(
            @Parameter(description = "分类", example = "recent") @RequestParam(value = "category", required = false) String category) {
        Long userId = getLoginUserId();
        audioService.clearPlayRecords(userId, category);
        return success(true);
    }

    @DeleteMapping("/play-record/{audioId}")
    @Operation(summary = "删除单个音频播放记录")
    @PreAuthenticated
    public CommonResult<Boolean> deletePlayRecord(
            @Parameter(description = "音频ID", required = true, example = "1") @PathVariable("audioId") Long audioId) {
        Long userId = getLoginUserId();
        audioService.deletePlayRecord(userId, audioId);
        return success(true);
    }

    @GetMapping("/book/{bookId}")
    @Operation(summary = "获取绘本相关音频")
    @PreAuthenticated
    public CommonResult<List<AudioResourceRespVO>> getAudiosByBook(
            @Parameter(description = "绘本ID", required = true, example = "1") @PathVariable("bookId") Long bookId) {
        Long userId = getLoginUserId();
        List<AudioResourceRespVO> result = audioService.getAudiosByBookId(userId, bookId);
        return success(result);
    }

    @GetMapping("/book-set/{bookSetId}")
    @Operation(summary = "获取绘本集相关音频")
    @PreAuthenticated
    public CommonResult<List<AudioResourceRespVO>> getAudiosByBookSet(
            @Parameter(description = "绘本集ID", required = true, example = "1") @PathVariable("bookSetId") Long bookSetId) {
        Long userId = getLoginUserId();
        List<AudioResourceRespVO> result = audioService.getAudiosByBookSetId(userId, bookSetId);
        return success(result);
    }

}
